package com.zryc.shop.pms.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.base.Result;
import com.zryc.shop.pms.entity.ProductAttribute;
import com.zryc.shop.pms.entity.ProductAttributeCategory;
import com.zryc.shop.pms.mapper.ProductAttributeCategoryMapper;
import com.zryc.shop.pms.mapper.ProductAttributeMapper;
import com.zryc.shop.pms.service.IProductAttributeCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zryc.shop.pms.service.IProductAttributeValueService;
import com.zryc.shop.pms.service.IProductCategoryAttributeRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品属性分类表 服务实现类
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-18
 */
@Service
public class ProductAttributeCategoryServiceImpl extends ServiceImpl<ProductAttributeCategoryMapper, ProductAttributeCategory> implements IProductAttributeCategoryService {


    private final IProductCategoryAttributeRelationService productCategoryAttributeRelationService;
    private final IProductAttributeValueService productAttributeValueService;
    private final ProductAttributeCategoryMapper productAttributeCategoryMapper;
    private final ProductAttributeMapper productAttributeMapper;


    public ProductAttributeCategoryServiceImpl(ProductAttributeCategoryMapper productAttributeCategoryMapper, ProductAttributeMapper productAttributeMapper, IProductAttributeValueService productAttributeValueService, IProductCategoryAttributeRelationService productCategoryAttributeRelationService) {
        this.productAttributeCategoryMapper = productAttributeCategoryMapper;
        this.productAttributeMapper = productAttributeMapper;
        this.productAttributeValueService = productAttributeValueService;
        this.productCategoryAttributeRelationService = productCategoryAttributeRelationService;
    }

    /**
     * 分页查询列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<ProductAttributeCategory> categoryList(Integer pageNum, Integer pageSize) {
        Page<ProductAttributeCategory> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ProductAttributeCategory> wrapper = new QueryWrapper<>();
        return page(page, wrapper);
    }

    /**
     * 修改属性分类名称
     *
     * @param id
     * @param name
     * @return
     */
    @Override
    public Object categoryUpdateById(Long id, String name) {
        UpdateWrapper<ProductAttributeCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("name", name);
        update(updateWrapper);
        return null;
    }

    /**
     * 创建属性分类
     *
     * @param name
     * @return
     */
    @Override
    public Object create(String name) {
        if (StringUtils.isEmpty(name)) {
            return Result.error(666, "属性分类名称不能为空");
        }
        ProductAttributeCategory attributeCategory = new ProductAttributeCategory();
        attributeCategory.setName(name);
        return super.save(attributeCategory);

    }

    @Override
    public List<ProductAttributeCategory> withAttr() {
        QueryWrapper<ProductAttributeCategory> wrapper = new QueryWrapper<>();
        List<ProductAttributeCategory> list = new ArrayList<>();
        List<ProductAttributeCategory> productAttributeCategories = productAttributeCategoryMapper.selectList(wrapper);
        for(ProductAttributeCategory productAttributeCategory : productAttributeCategories) {
            List<ProductAttribute> productAttributes = productAttributeMapper.selectPIDById(productAttributeCategory.getId());
            productAttributeCategory.setProductAttributeList(productAttributes);
            list.add(productAttributeCategory);
        }
        return list;
    }


    @Override
    public Object deleteAttributeCategory(Long id) {
        baseMapper.deleteById(id);
        QueryWrapper<ProductAttribute> productAttributeQueryWrapper = new QueryWrapper<>();
        productAttributeQueryWrapper.eq("product_attribute_category_id",id);
        List<ProductAttribute> productAttributeList = productAttributeMapper.selectList(productAttributeQueryWrapper);
        for (ProductAttribute productAttribute : productAttributeList) {
            Long productAttributeId = productAttribute.getId();
            baseMapper.deleteForAttributeValue(productAttributeId);
            baseMapper.removeFromCAR(productAttributeId);
        }
        baseMapper.deleteForAttribute(id);
        return null;
    }


}
